/**
 * 
 */
package name.marz.to.go.btree;

import name.marz.to.go.btree.node.ArrayNode;
import name.marz.to.go.btree.node.BTree;
import name.marz.to.go.btree.node.Key;
import name.marz.to.go.btree.node.Node;
import name.marz.to.go.btree.node.NodeFactory;
import name.marz.to.go.btree.node.NodeOverflowException;
import test.Data;

/**
 * @author masaru-gotou
 *
 */
public class DemoTreeBuilder {
	public BTree<Data> buildTree() {
		int keycount = 4;
		Node root = NodeFactory.createNode(ArrayNode.class.getName(),keycount);
		Node leaf1 = NodeFactory.createNode(ArrayNode.class.getName(),keycount);
		Node leaf2 = NodeFactory.createNode(ArrayNode.class.getName(),keycount);
		Node leaf3 = NodeFactory.createNode(ArrayNode.class.getName(),keycount);
		try {
			root.setLeftPointer(leaf1);
			root.add(new Key(23), leaf2);
			root.add(new Key(71), leaf3);
			leaf1.add(new Key(8), new Data(8));
			leaf1.add(new Key(11), new Data(11));
			leaf1.add(new Key(19), new Data(19));
			leaf2.add(new Key(34), new Data(34));
			leaf2.add(new Key(44), new Data(44));
			leaf2.add(new Key(56), new Data(56));
			leaf2.add(new Key(57), new Data(57));
			leaf3.add(new Key(75), new Data(75));
			leaf3.add(new Key(92), new Data(92));
		} catch (NodeOverflowException e) {
			e.printStackTrace();
		}
		return new BTree<Data>(root);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DemoTreeBuilder builder = new DemoTreeBuilder();
		BTree<Data> tree = builder.buildTree();
		
		Data data = (Data)tree.search(new Key(19));
		System.out.println("Data="+data);
		data = (Data)tree.search(new Key(56));
		System.out.println("Data="+data);
		data = (Data)tree.search(new Key(100));
		System.out.println("Data="+data);
	}

}
